/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Lee
 */
public class GetBorrowers extends ActionSupport {

    private BorrowerDAO bord = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private String[] brList;
    private List<String> brList1;

    public String[] getBrList() {
        return brList;
    }

    @Action(value = "/get-borrowers", results = {
        @Result(name = "input", location = "/loanapplication.jsp"),
        @Result(name = "success", location = "/loanapplication.jsp"),
        @Result(name = "error", location = "/loanapplication.jsp")
    })
    @Override
    public String execute() {
        try {
//            System.out.println("yes");
            brList1 = new ArrayList<String>();
            List<Borrowers> lb = bord.getActiveBorrowers();

            if (!bord.getActiveBorrowers().isEmpty()) {
                for (int i = 0; i < lb.size(); i++) {
                    String id = lb.get(i).getBorrowerid().toString();
                    String fn = lb.get(i).getFirstname();
                    String mn = lb.get(i).getMiddlename();
                    String ln = lb.get(i).getLastname();
                    String borcode = lb.get(i).getBorrowercode();
                    String info = borcode + " " + fn + " " + mn + " " + ln;
//                    System.out.println(info);
                    brList1.add(info);
                }

//        ServletActionContext.getRequest().setAttribute("cat", "appli");
                brList = brList1.toArray(new String[brList1.size()]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;

    }
}
